namespace SimpleWebServices.Web
{
    using System;
    using System.Collections.Specialized;

    public static class StringExtensionMethods
    {
        public static string ToAppRelativeCurrentExecutionFilePath(this string path)
        {
            string value = path;

            if ((null != path) && path.Contains("?"))
            {
                value = path.Substring(0, path.IndexOf("?", StringComparison.OrdinalIgnoreCase));
            }

            return value;
        }

        public static NameValueCollection ToQueryString(this string path)
        {
            NameValueCollection collection = null;

            if ((null != path) && path.Contains("?"))
            {
                collection = new NameValueCollection();

                string[] parameters = path.Split(new char[] { '?' })[1].Split(new char[] { '&' });

                foreach (string parameter in parameters)
                {
                    string[] parts = parameter.Split(new char[] { '=' });
                    collection.Add(parts[0], parts[1]);
                }
            }

            return collection;
        }
    }
}